package com.eastfair.common.controller.micetother;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.StopWatch;
import com.alibaba.fastjson.JSONObject;
import com.eastfair.certificate.api.CertificateApiFeign;
import com.eastfair.certificate.dto.CertificateIndexDTO;
import com.eastfair.certificate.dto.CertificateUserDTO;
import com.eastfair.common.req.CertificateIndexForLiFangReq;
import com.eastfair.common.req.CertificateIndexReq;
import com.eastfair.common.service.CommonInterfaceCallRecordService;
import com.eastfair.common.vo.CommonExhibitionManageVO;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venuebooking.api.ExhibitionManageFeign;
import com.eastfair.venueservice.vo.ExhibitionManageVO;
import core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassNameCertificateTother
 * @Description 制证对物流车
 * @Authoradmin
 * @Date 2022/12/26 14:43
 * @Version 1.0
 **/
@Service
@Slf4j
public class CertificateLogisticsSyncTask {

    @Resource
    private CommonInterfaceCallRecordService commonInterfaceCallRecordService;


    @Value("${roma.ipport}")
    private String romaIpport;

    //同步展会
    @Value("${logistics.exhibition.url}")
    private String logisticsExhibitionUrl;
    //同步证件
    @Value("${logistics.certificate.url}")
    private String logisticsCertificateUrl;
    //同步车辆预定
    @Value("${logistics.certificate_index.url}")
    private String logisticsCertificateIndexUrl;

    //同步车辆预定
    @Value("${parking.certificate_index_lifang.url}")
    private String logisticsCertificateIndexLifangUrl;

    @Value("${roma.LOGISTICS-X-HW-ID}")
    private String romaXHWID;

    @Value("${roma.LOGISTICS-X-HW-APPKEY}")
    private String romaXHWAPPKEY;

    @Value("${roma.PARKING-X-HW-ID}")
    private String parkingRomaXHWID;

    @Value("${roma.PARKING-X-HW-APPKEY}")
    private String parkingRomaXHWAPPKEY;

    @Value("${common.projectId}")
    private Long commonProjectId;


    @Resource
    private ExhibitionManageFeign exhibitionManageFeign;

    @Resource
    private CertificateApiFeign certificateApiFeign;


    /**
     * @return void
     * @Author clm
     * @Description //同步展会
     * @Date 15:09 2023/1/3
     * @Param []
     **/
//    @Scheduled(cron="0/100 * * * * *")
    @XxlJob("syncExhibition")
    public void syncExhibition() {
        ContextUtil.setProjectId(commonProjectId);
        R<List<ExhibitionManageVO>> listR = exhibitionManageFeign.queryNeedSyncExhibitionManage();
        List<CommonExhibitionManageVO> commonExhibitionManageList = new ArrayList<>();
        if (listR.getIsSuccess()) {
            List<ExhibitionManageVO> exhibitionManageList = listR.getData();
            commonExhibitionManageList = exhibitionManageList.stream()
                    .map(exhibitionManageVO -> BeanUtil.toBean(exhibitionManageVO, CommonExhibitionManageVO.class))
                    .collect(Collectors.toList());
        }
        if (CollectionUtils.isEmpty(commonExhibitionManageList)) {
            return;
        }
        String httpUrl = romaIpport + logisticsExhibitionUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncExhibition");
        log.info("===================================syncExhibition start=======================================");
        commonInterfaceCallRecordService.processHttp(httpUrl, commonExhibitionManageList, romaXHWID, romaXHWAPPKEY);
        log.info("===================================syncExhibition end=======================================");
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
    }

    /**
     * @return void
     * @Author clm
     * @Description //同步车证
     * @Date 16:07 2023/1/5
     * @Param []
     **/
//    @Scheduled(cron="0/30 * * * * *")
    @XxlJob("syncCertificate")
    public void syncCertificate() {
        ContextUtil.setProjectId(commonProjectId);
        log.info("syncCertificate  is execute");
        R<List<CertificateUserDTO>> listR = certificateApiFeign.sycnCertificate();
        log.info("syncCertificate list is {}", JSONObject.toJSONString(listR));
        if (!listR.getIsSuccess() || CollectionUtils.isEmpty(listR.getData())) {
            return;
        }
        List<CertificateUserDTO> certificateVOS = listR.getData();
        String httpUrl = romaIpport + logisticsCertificateUrl;
        log.info("url is {}", httpUrl);
        StopWatch sw = new StopWatch();
        sw.start("syncCertificate");
        log.info("===================================syncCertificate start=======================================");
        commonInterfaceCallRecordService.processHttp(httpUrl, certificateVOS, null, "1234567890", romaXHWID, romaXHWAPPKEY);
        log.info("===================================syncCertificate end=======================================");
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());

    }


    /**
     * @return void
     * @Author clm
     * @Description //同步车辆预定
     * @Date 16:07 2023/1/5
     * @Param []
     **/
//    @Scheduled(cron="0/10 * * * * *")
    @XxlJob("syncCertificateAppointment")
    public void syncCertificateAppointment() {
        ContextUtil.setProjectId(commonProjectId);
        CertificateIndexDTO certificateIndexDTO = new CertificateIndexDTO();
        R<List<CertificateIndexReq>> listR = certificateApiFeign.sycnCertificateIndex(certificateIndexDTO);
        log.info("certificateIndexListcertificateIndexList  is {} ", JSONObject.toJSONString(listR));
        if (!listR.getIsSuccess() || CollectionUtils.isEmpty(listR.getData())) {
            return;
        }
        List<CertificateIndexReq> certificateIndexList = listR.getData();
        String httpUrl = romaIpport + logisticsCertificateIndexUrl;
        String httpUrlLifang = romaIpport + logisticsCertificateIndexLifangUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncCertificateAppointment");
        log.info("===================================syncCertificateAppointment start=======================================");
        log.info("httpUrl  is {} ", httpUrl);
        log.info("certificateIndexListcertificateIndexList  is {} ", JSONObject.toJSONString(certificateIndexList));
        commonInterfaceCallRecordService.processHttp(httpUrl, certificateIndexList, null, "1234567890", romaXHWID, romaXHWAPPKEY);
        log.info("===================================syncCertificateAppointment end=======================================");
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());

        sw.start("syncCertificateAppointmentlifang");
        log.info("===================================syncCertificateAppointmentlifang start=======================================");
        for (CertificateIndexReq certificateIndexReq : certificateIndexList) {
            CertificateIndexForLiFangReq certificateIndexForLiFangReq = new CertificateIndexForLiFangReq();
            certificateIndexForLiFangReq.setAct(certificateIndexReq.getAct());
            certificateIndexForLiFangReq.setBeginTime(certificateIndexReq.getStartTime());
            certificateIndexForLiFangReq.setEndTime(certificateIndexReq.getEndTime());
            certificateIndexForLiFangReq.setCarCode(certificateIndexReq.getCarCode());
            certificateIndexForLiFangReq.setCertificateId(certificateIndexReq.getCertificateId());
            commonInterfaceCallRecordService.processHttp(httpUrlLifang, certificateIndexForLiFangReq, null, "1234567890", parkingRomaXHWID, parkingRomaXHWAPPKEY);
        }
        log.info("===================================syncCertificateAppointmentlifang end=======================================");
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());

    }

}
